using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Input;

namespace Supertris
{
    static class Input
    {
        public static KeyboardState keyboard;
        public static KeyboardState lastKeyboard;
        static Queue<KeyboardState> delayKeyboard = new Queue<KeyboardState>();
        static int delayFrames = 10;

        public static void Update()
        {
            lastKeyboard = keyboard;
            keyboard = Keyboard.GetState();

            if (delayKeyboard.Count >= delayFrames)
                delayKeyboard.Dequeue();
            delayKeyboard.Enqueue(lastKeyboard);
        }

        public static bool KeyHit(Keys key)
        {
            return (keyboard.IsKeyDown(key) && lastKeyboard.IsKeyUp(key));
        }

        public static bool KeyHeld(Keys key)
        {
            if (KeyHit(key))
                return true;

            foreach (KeyboardState state in delayKeyboard)
            {
                if (state.IsKeyUp(key))
                    return false;
            }

            return true;
        }
    }
}
